<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>添加文章</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../css/oksub.css">
	<link rel="stylesheet" href="../../lib/EditorMD/css/editormd.min.css" />
	<script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
	<style>
		.thumbImg {
			width: 200px;
			height: 200px;
			margin: 0 10px 10px 0;
		}

		.thumbBox{
			text-align: center;
			position: relative;
		}

		.thumbBox:after{
			position: absolute;
			width: 100%;
			height: 100%;
			line-height: 185px;
			z-index: -1;
			text-align: center;
			font-size: 20px;
			content: "点击上传封面";
			left: 0;
			top: 0;
			color: #9F9F9F;
		}
	</style>
</head>
<body>
<div class="ok-body">
	<!--form表单-->
	<form class="layui-form layui-form-pane ok-form" lay-filter="form">
		<div class="layui-upload">
			<div class="layui-upload-list thumbBox">
				<img class="layui-upload-img thumbImg" id="uploadCover">
				<input name="coverUrl" id="coverUrl" type="hidden">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">标题</label>
			<div class="layui-input-block">
				<input type="text" name="title" placeholder="请输入文章标题" autocomplete="off" class="layui-input"
				       lay-verify="required">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">分类</label>
			<div class="layui-input-block">
				<div id="categoryId" class="xm-select-demo"></div>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">标签</label>
			<div class="layui-input-block">
				<div id="label" class="xm-select-demo"></div>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">状态</label>
			<div class="layui-input-block">
				<select name="status" id="status"  lay-filter="status">
					<option value="">请选择</option>
					<option value="0">立即发布</option>
					<option value="1">定时发布</option>
					<option value="2">保存草稿</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item" id="releaseTimeDiv" style="display: none">
			<label class="layui-form-label">发布时间</label>
			<div class="layui-input-block">
				<input type="text" name="releaseTime" id="releaseTime" placeholder="请选择发布时间" autocomplete="off" class="layui-input"
					  >
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">置顶</label>
			<div class="layui-input-block">
				<input type="checkbox" name="istop" lay-skin="switch" lay-text="是|否" value="0">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">是否公开</label>
			<div class="layui-input-block">
				<input type="checkbox" name="ispublic" id="ispublic" lay-skin="switch" lay-text="是|否" value="0">
			</div>
		</div>
		<!--<div class="layui-form-item layui-form-text">-->
			<!--<div class="layui-input-block" id="editormd">-->
				<!--&lt;!&ndash;<textarea name="content" placeholder="请输入文章内容" class="layui-textarea" lay-verify="required"></textarea>&ndash;&gt;-->
				<!--<textarea style="display:none;"></textarea>-->
			<!--</div>-->
		<!--</div>-->
		<div class="editormd" id="editormd">
			<!--<textarea name="content" placeholder="请输入文章内容" class="layui-textarea" lay-verify="required"></textarea>-->
			<textarea style="display:none;"></textarea>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit lay-filter="addArticle">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
</div>
<!--js逻辑-->
<script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="../../lib/EditorMD/js/editormd.min.js"></script>
<script type="text/javascript" src="../../lib/EditorMD/js/uploadImg.js"></script>
<script src="../../lib/layui/layui.js"></script>
<script src="../../lib/xmSelect/xm-select.js"></script>
<script>
	layui.use(["form", "okUtils", "okLayer","upload",'jquery','laydate'], function () {
		let form = layui.form;
		let okUtils = layui.okUtils;
		let okLayer = layui.okLayer;
		let upload=layui.upload;
		let $=layui.jquery;
		let laydate=layui.laydate;
		okLoading.close();
		$(function(){
            showMd();
		});
        var editor;
		//初始化日期表单
        laydate.render({
            elem: '#releaseTime'
            ,type: 'datetime'
        });
		//监听是否定时发布
        form.on('select(status)', function(data){
            console.log(data);
			if(data.value==1){
			    $('#releaseTimeDiv').show();
			} else{
			    $('#releaseTimeDiv').hide();
			}
        });

		form.on("submit(addArticle)", function (data) {
		    var data=data.field;
		    if(data.releaseTime==""){
		        data.releaseTime=null;
			}
            data.labelIds=labelSelect.getValue('valueStr');
            data.categoryId=categorySelect.getValue('value')[0];
            data.content=editor.getMarkdown();
            console.log(data);
			okUtils.ajax("/article/add", "post", data, true).done(function (response) {
				okLayer.greenTickMsg(response.message, function () {
					parent.layer.close(parent.layer.getFrameIndex(window.name));
				});
			}).fail(function (error) {
				console.log(error)
			});
			return false;
		});

		//图片上传
        upload.render({
            elem: '#uploadCover'
            ,url: '/api/file/uploadArticle' //改成您自己的上传接口
            ,multiple: true
            ,before: function(obj){
                loadIndex=layer.load(0, {shade: 0.3});
                //预读本地文件示例，不支持ie8
                // obj.preview(function(index, file, result){
                //     $('#uploadCover').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                // });
            }
            ,done: function(res){
                //上传完毕
                form.val('form',{
                    "coverUrl":res.url,
                });
				$('#uploadCover').attr("src",res.url);
                layer.close(loadIndex);
                layer.msg(res.msg, {icon: 7, time: 2000});
            }
        });

		//文章标签
        var labelSelect=xmSelect.render({
            el: '#label',
            paging: true,
            data: [
                {name: '张三', value: 1},
                {name: '李四', value: 2},
                {name: '王五', value: 3},
                {name: '赵六', value: 4},
                {name: '苹果', value: 5},
                {name: '香蕉', value: 6},
                {name: '凤梨', value: 7},
                {name: '葡萄', value: 8},
                {name: '樱桃', value: 9},
                {name: '车厘子', value: 10},
                {name: '火龙果', value: 11},
            ]
        });

        okUtils.ajax("/article/allLabel", "get", null, true).done(function (response) {
            if(response.status==200){
                labelSelect.update({
                    data: response.data,
                    autoRow: true,
                })
			}
        }).fail(function (error) {
            console.log(error)
        });

        //分类下拉
        var categorySelect = xmSelect.render({
            el: '#categoryId',
            radio: true,
            clickClose: true,
            paging: true,
            data: [
                {name: '张三', value: 1},
                {name: '李四', value: 2},
                {name: '王五', value: 3},
            ]
        });

        okUtils.ajax("/article/allCategory", "get", null, true).done(function (response) {
            if(response.status==200){
                categorySelect.update({
                    data: response.data,
                    autoRow: true,
                })
            }
        }).fail(function (error) {
            console.log(error)
        });

        function showMd(){
            editor = editormd("editormd", {
                width  : "100%",
                height : "300px",
                saveHTMLToTextarea : true,
                imageUpload : true,
                imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                imageUploadURL : "/api/file/uploadArticle",
                onload: function () {

                    initPasteDragImg(this);
                },
                path : "../../lib/EditorMD/lib/"
            });
        }
	});
</script>
</body>
</html>
